package com.chisheng.student.servlet;

import com.chisheng.student.Student;
import com.chisheng.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 查询学生请求处理
 *
 * @author chisheng
 * @since 2021-11-05
 */
@WebServlet("/query")
public class QueryServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String id = request.getParameter("id");
        if (id != null && !"".equals(id)) {
            // 查询学生
            StudentServiceImpl studentService = new StudentServiceImpl();
            Student student = studentService.query(Integer.parseInt(id));
            if (student != null) {
                request.setAttribute("status", true);
                request.setAttribute("message", "数据回显成功");
                request.setAttribute("student", student);
            } else {
                request.setAttribute("status", false);
                request.setAttribute("message", "数据回显失败");
            }
        } else {
            request.setAttribute("status", false);
            request.setAttribute("message", "参数异常");
        }

        // 请求转发
        String path = request.getContextPath();
        RequestDispatcher dispatcher = request.getRequestDispatcher(path + "/edit.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
